package com.ruoyi.web.controller.system;

import com.ruoyi.web.service.IntegratedInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin(origins = "*") // 允许所有来源的跨域请求
@RestController
@RequestMapping("/system/integratedInfo")
public class IntegratedInfoController {

    @Autowired
    private IntegratedInfoService integratedInfoService;

    @GetMapping("/c2info")
    public Map<String, List<Object>> getc2infoData() {
        List<Map<String, Object>> rawData = integratedInfoService.getC2VersionInfo();

        Map<String, List<Object>> response = new HashMap<>();
        List<Object> buildTimes = new ArrayList<>();
        List<Object> statuses = new ArrayList<>();
        List<Object> releaseStatuses = new ArrayList<>();
        List<Object> pubStatuses = new ArrayList<>();

        for (Map<String, Object> data : rawData) {
            // 确保 "buildTime" 字段存在且为日期/时间类型
            if (data.get("buildTime") != null) {
                buildTimes.add(data.get("buildTime"));
            } else {
                System.out.println("Data format issue with 'buildTime' field: " + data.get("buildTime"));
                buildTimes.add("");  // 或者可以选择跳过该数据
            }

            // 确保 "status", "releaseStatus", "pubStatus" 为整数类型
            try {
                statuses.add(Integer.parseInt(data.get("status").toString()));
                releaseStatuses.add(Integer.parseInt(data.get("releaseStatus").toString()));
                pubStatuses.add(Integer.parseInt(data.get("pubStatus").toString()));
            } catch (NumberFormatException e) {
                System.out.println("Data format issue with status fields: " + data);
                statuses.add(0);
                releaseStatuses.add(0);
                pubStatuses.add(0);
            }
        }

        response.put("buildTimes", buildTimes);
        response.put("statuses", statuses);
        response.put("releaseStatuses", releaseStatuses);
        response.put("pubStatuses", pubStatuses);

        return response;
    }

    @GetMapping("/weekly-status")
    public Map<String, Object> getWeeklyStats() {
        // 调用服务层方法，获取按周统计的数据
        Map<String, Object> weeklyData = integratedInfoService.getC2VersionInfoByWeek();

        // 返回数据已经是需要的格式，直接返回
        // 打印返回的数据
        System.out.println("返回的数据: " + weeklyData);
        return weeklyData;
    }


    @GetMapping("/integrateComponentInfo")
    public Map<String, List<Object>> getIntegrateComponentInfoData() {
        List<Map<String, Object>> rawData = integratedInfoService.getIntegrateComponentInfo();

        Map<String, List<Object>> response = new HashMap<>();
        List<Object> buildTimes = new ArrayList<>();
        List<Object> statuses = new ArrayList<>();

        for (Map<String, Object> data : rawData) {
            // 确保 "buildTime" 字段存在且为日期/时间类型
            if (data.get("buildTime") != null) {
                buildTimes.add(data.get("buildTime"));
            } else {
                System.out.println("Data format issue with 'buildTime' field: " + data.get("buildTime"));
                buildTimes.add("");  // 或者可以选择跳过该数据
            }

            // 确保 "status" 为整数类型
            try {
                statuses.add(Integer.parseInt(data.get("status").toString()));
            } catch (NumberFormatException e) {
                System.out.println("Data format issue with 'status' field: " + data);
                statuses.add(0);
            }
        }

        response.put("buildTimes", buildTimes);
        response.put("statuses", statuses);

        return response;
    }
}
